<?php
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}

require_once('lib/db_connection.php');
$company_id = $_SESSION['company_id'];
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];

if($p2=='LIKE')
$passed_where = $p1." ".$p2." '%".$p3."%'";
if($p2!='LIKE')
$passed_where = $p1." ".$p2." '".$p3."'";

	$filename = "Vehicle_Report".date('Y-m-d');
    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls "); // แล้วนี่ก็ชื่อไฟล์
    header("Content-Transfer-Encoding: binary ");

	
	
   // Query Database
    if($p3==''||$p3==null)
	{
	$query_veh = "SELECT 
	vehicle_tbl.*,vehicle_tbl.chassis_num as c_num,vehicle_tbl.year as v_year,vehicle_tbl.customer as v_cust,
	veh_purchase_tbl.*,veh_purchase_tbl.date as p_date,veh_purchase_tbl.price as p_price,veh_purchase_tbl.expiration_date as p_exp,
	veh_engine_tbl.*,
	veh_insurance_tbl.*,
	veh_registration_tbl.*,veh_registration_tbl.date as r_date,
	veh_depreciation_tbl.*,
	veh_fluids_tbl.*,
	gps_tbl.*,
	veh_disposal_tbl.*,
	veh_chassis_tbl.*,
	veh_other_details_tbl.* 
	FROM vehicle_tbl 
	LEFT JOIN veh_purchase_tbl 
	ON vehicle_tbl.vehicle_id=veh_purchase_tbl.vehicle_id
	LEFT JOIN veh_engine_tbl 
	ON vehicle_tbl.vehicle_id=veh_engine_tbl.vehicle_id
	LEFT JOIN veh_insurance_tbl 
	ON vehicle_tbl.vehicle_id=veh_insurance_tbl.vehicle_id
	LEFT JOIN veh_registration_tbl 
	ON vehicle_tbl.vehicle_id=veh_registration_tbl.vehicle_id
	LEFT JOIN veh_depreciation_tbl 
	ON vehicle_tbl.vehicle_id=veh_depreciation_tbl.vehicle_id
	LEFT JOIN veh_fluids_tbl 
	ON vehicle_tbl.vehicle_id=veh_fluids_tbl.vehicle_id
	LEFT JOIN gps_tbl 
	ON vehicle_tbl.vehicle_id=gps_tbl.vehicle_id
	LEFT JOIN veh_disposal_tbl 
	ON vehicle_tbl.vehicle_id=veh_disposal_tbl.vehicle_id
	LEFT JOIN veh_chassis_tbl 
	ON vehicle_tbl.vehicle_id=veh_chassis_tbl.vehicle_id
	LEFT JOIN veh_other_details_tbl 
	ON vehicle_tbl.vehicle_id=veh_other_details_tbl.vehicle_id
	WHERE vehicle_tbl.company_id='".$company_id."'";
	}

	if($p3!=''||$p3!=null)
	{
	$query_veh = "SELECT 
	vehicle_tbl.*,vehicle_tbl.chassis_num as c_num,vehicle_tbl.year as v_year,vehicle_tbl.customer as v_cust,
	veh_purchase_tbl.*,veh_purchase_tbl.date as p_date,veh_purchase_tbl.price as p_price,veh_purchase_tbl.expiration_date as p_exp,
	veh_engine_tbl.*,
	veh_insurance_tbl.*,
	veh_registration_tbl.*,veh_registration_tbl.date as r_date,
	veh_depreciation_tbl.*,
	veh_fluids_tbl.*,
	gps_tbl.*,
	veh_disposal_tbl.*,
	veh_chassis_tbl.*,
	veh_other_details_tbl.* 
	FROM0 vehicle_tbl 
	LEFT JOIN veh_purchase_tbl 
	ON vehicle_tbl.vehicle_id=veh_purchase_tbl.vehicle_id
	LEFT JOIN veh_engine_tbl 
	ON vehicle_tbl.vehicle_id=veh_engine_tbl.vehicle_id
	LEFT JOIN veh_insurance_tbl 
	ON vehicle_tbl.vehicle_id=veh_insurance_tbl.vehicle_id
	LEFT JOIN veh_registration_tbl 
	ON vehicle_tbl.vehicle_id=veh_registration_tbl.vehicle_id
	LEFT JOIN veh_depreciation_tbl 
	ON vehicle_tbl.vehicle_id=veh_depreciation_tbl.vehicle_id
	LEFT JOIN veh_fluids_tbl 
	ON vehicle_tbl.vehicle_id=veh_fluids_tbl.vehicle_id
	LEFT JOIN gps_tbl 
	ON vehicle_tbl.vehicle_id=gps_tbl.vehicle_id
	LEFT JOIN veh_disposal_tbl 
	ON vehicle_tbl.vehicle_id=veh_disposal_tbl.vehicle_id
	LEFT JOIN veh_chassis_tbl 
	ON vehicle_tbl.vehicle_id=veh_chassis_tbl.vehicle_id
	LEFT JOIN veh_other_details_tbl 
	ON vehicle_tbl.vehicle_id=veh_other_details_tbl.vehicle_id
	WHERE vehicle_tbl.company_id='".$company_id."'
	AND ".$passed_where;		
	}
	$result_veh = mysql_query($query_veh)or die(mysql_error());
   
    // XLS Data Cell

                xlsBOF();
                /*xlsWriteLabel(1,0,"Student Register $semester/$year");
                xlsWriteLabel(2,0,"COURSENO : ");
                xlsWriteLabel(2,1,"$courseid");
                xlsWriteLabel(3,0,"TITLE : ");
                xlsWriteLabel(3,1,"$title");
                xlsWriteLabel(4,0,"SETION : ");
                xlsWriteLabel(4,1,"$sec");
                xlsWriteLabel(6,0,"Plate");
                xlsWriteLabel(6,1,"Model");
                xlsWriteLabel(6,2,"Manufacturer");
                xlsWriteLabel(6,3,"Track By");
                xlsWriteLabel(6,4,"Odometer/Hrs");
                $xlsRow = 7;*/
                
                //VEHICLE DETAILS
                xlsWriteLabel(0,0,"Plate No");
                xlsWriteLabel(0,1,"Year");
                xlsWriteLabel(0,2,"Make");
                xlsWriteLabel(0,3,"Model");
                xlsWriteLabel(0,4,"Total Mileage");
                xlsWriteLabel(0,5,"Ownership");
                xlsWriteLabel(0,6,"Owners Name");
                xlsWriteLabel(0,7,"Driver");
                xlsWriteLabel(0,8,"Engine No");
                xlsWriteLabel(0,9,"Chassis No");
                xlsWriteLabel(0,10,"Department");
                xlsWriteLabel(0,11,"Garage");
                
                //PURCHASE
                xlsWriteLabel(0,12,"Purchase Date");
                xlsWriteLabel(0,13,"Price");
                xlsWriteLabel(0,14,"Purchase From");
                xlsWriteLabel(0,15,"End of Warranty");
                
                //ENGINE
                xlsWriteLabel(0,16,"Code");
                xlsWriteLabel(0,17,"Power(bhp)");
                xlsWriteLabel(0,18,"Torque(lb-ft)");
                xlsWriteLabel(0,19,"Disp(ci)");
                xlsWriteLabel(0,20,"Aircon Type");
                xlsWriteLabel(0,21,"Aspiration");
                xlsWriteLabel(0,22,"Cylinder");
                xlsWriteLabel(0,23,"Fuel Type");
                
                //CHASSIS
				xlsWriteLabel(0,24,"Diff Type");
                xlsWriteLabel(0,25,"Trans Type");
                xlsWriteLabel(0,26,"Trans Code");
                xlsWriteLabel(0,27,"Ratio");
                xlsWriteLabel(0,28,"Front Wheel Size");
                xlsWriteLabel(0,29,"Front Tire Size");
                xlsWriteLabel(0,30,"Rear Wheel Size");
                xlsWriteLabel(0,31,"Rear Tire Size");
                
                //FLUIDS
				xlsWriteLabel(0,32,"Oil Type");
                xlsWriteLabel(0,33,"Transmission Fluid");
                xlsWriteLabel(0,34,"Freon");
                xlsWriteLabel(0,35,"Gear Oil");
                xlsWriteLabel(0,36,"Hydraulic Fluid");
                
                //LTO REGISTRATION
				xlsWriteLabel(0,37,"Field Office");
                xlsWriteLabel(0,38,"CR No");
                xlsWriteLabel(0,39,"Registration Date");
                xlsWriteLabel(0,40,"OR No");
                xlsWriteLabel(0,41,"OR Date");
                
                //DISPOSAL
				xlsWriteLabel(0,42,"Disposal Date");
                xlsWriteLabel(0,43,"Sold To");
                xlsWriteLabel(0,44,"Address");
                xlsWriteLabel(0,45,"Price");
                
                //DEPRECIATION
				xlsWriteLabel(0,46,"Depreciation Description");
                xlsWriteLabel(0,47,"Total Period");
                xlsWriteLabel(0,48,"Current Value");
                xlsWriteLabel(0,49,"Depreciation per Period");
                
                //GPS
				xlsWriteLabel(0,50,"GPS ID");
                xlsWriteLabel(0,51,"Sim No");
                xlsWriteLabel(0,52,"Device");
                
                //OTHER DETAILS
				xlsWriteLabel(0,53,"w/ Fleet Card");
                xlsWriteLabel(0,54,"ID No");
                xlsWriteLabel(0,55,"Band");
                xlsWriteLabel(0,56,"Benefit Plan");
                xlsWriteLabel(0,57,"Cost Center");
                xlsWriteLabel(0,58,"Group");
                xlsWriteLabel(0,59,"Division");
                xlsWriteLabel(0,60,"Section");
                xlsWriteLabel(0,61,"Ref PO");
                xlsWriteLabel(0,62,"Asset No");
                xlsWriteLabel(0,63,"Equipt No");
                
                
                $xlsRow = 1;
                while($row_veh=mysql_fetch_array($result_veh)) {
                    ++$i;
                    
            	//TRACKING
				$tracking = $row_veh['track_by'];
				if($tracking=='distance')
				$curr_od = $row_veh['curr_tracking_value'].'Km';
				if($tracking=='hours')
				$curr_od = $row_veh['curr_hours'].'Hrs';
			
				//FLEET CARD
				$f_card = $row_veh['fleet_card'];
				if($f_card==1)
				$fcard = 'Yes';
				if($f_card==0)
				$fcard = 'No';
				if($f_card==null)
				$fcard = ' ';
                
				//xlsWriteNumber
				//VEHICLE DETAILS    
                xlsWriteLabel($xlsRow,0,$row_veh['veh_num']);
                xlsWriteLabel($xlsRow,1,$row_veh['v_year']);
                xlsWriteLabel($xlsRow,2,$row_veh['manufacturer_name']);
                xlsWriteLabel($xlsRow,3,$row_veh['model_name']);
                xlsWriteLabel($xlsRow,4,$curr_od);
                xlsWriteLabel($xlsRow,5,$row_veh['ownership']);
                xlsWriteLabel($xlsRow,6,$row_veh['customer']);
                xlsWriteLabel($xlsRow,7,$row_veh['operator']);
                xlsWriteLabel($xlsRow,8,$row_veh['engine_model']);
                xlsWriteLabel($xlsRow,9,$row_veh['c_num']);
                xlsWriteLabel($xlsRow,10,$row_veh['department']);
                xlsWriteLabel($xlsRow,11,$row_veh['location']);
                
                //PURCHASE    
                xlsWriteLabel($xlsRow,12,$row_veh['p_date']);
                xlsWriteLabel($xlsRow,13,$row_veh['p_price']);
                xlsWriteLabel($xlsRow,14,$row_veh['purchase_from']);
                xlsWriteLabel($xlsRow,15,$row_veh['p_exp']);
                
                //ENGINE
				xlsWriteLabel($xlsRow,16,$row_veh['code']);
                xlsWriteLabel($xlsRow,17,$row_veh['power_bhp']);
                xlsWriteLabel($xlsRow,18,$row_veh['torque_lb']);
                xlsWriteLabel($xlsRow,19,$row_veh['disp_ci']);
                xlsWriteLabel($xlsRow,20,$row_veh['aircon_type']);
                xlsWriteLabel($xlsRow,21,$row_veh['aspiration']);
                xlsWriteLabel($xlsRow,22,$row_veh['cylinder']);
                xlsWriteLabel($xlsRow,23,$row_veh['fuel_type']);
                
                //CHASSIS
				xlsWriteLabel($xlsRow,24,$row_veh['diff_type']);
                xlsWriteLabel($xlsRow,25,$row_veh['trans_type']);
                xlsWriteLabel($xlsRow,26,$row_veh['trans_code']);
                xlsWriteLabel($xlsRow,27,$row_veh['ratio']);
                xlsWriteLabel($xlsRow,28,$row_veh['fwheel_size']);
                xlsWriteLabel($xlsRow,29,$row_veh['ftire_size']);
                xlsWriteLabel($xlsRow,30,$row_veh['rwheel_size']);
                xlsWriteLabel($xlsRow,31,$row_veh['rtire_size']);
                
                //FLUIDS
				xlsWriteLabel($xlsRow,32,$row_veh['oil_type']);
                xlsWriteLabel($xlsRow,33,$row_veh['transmission_fluid']);
                xlsWriteLabel($xlsRow,34,$row_veh['freon']);
                xlsWriteLabel($xlsRow,35,$row_veh['gear_oil']);
                xlsWriteLabel($xlsRow,36,$row_veh['hydraulic_fluid']);
                
                //LTO REGISTRATION
				xlsWriteLabel($xlsRow,37,$row_veh['field_office']);
                xlsWriteLabel($xlsRow,38,$row_veh['cr_no']);
                xlsWriteLabel($xlsRow,39,$row_veh['r_date']);
                xlsWriteLabel($xlsRow,40,$row_veh['or_no']);
                xlsWriteLabel($xlsRow,41,$row_veh['or_date']);
                
                //DISPOSAL
				xlsWriteLabel($xlsRow,42,$row_veh['sales_date']);
                xlsWriteLabel($xlsRow,43,$row_veh['buyer']);
                xlsWriteLabel($xlsRow,44,$row_veh['bldg'].' '.$row_veh['street'].' '.$row_veh['city'].','.$row_veh['country']);
                xlsWriteLabel($xlsRow,45,$row_veh['price']);
                
                //DEPRECIATION
				xlsWriteLabel($xlsRow,46,$row_veh['description']);
                xlsWriteLabel($xlsRow,47,$row_veh['tot_period']);
                xlsWriteLabel($xlsRow,48,$row_veh['curr_value']);
                xlsWriteLabel($xlsRow,49,$row_veh['dep_per_period']);
                
                //GPS
				xlsWriteLabel($xlsRow,50,$row_veh['gps_id']);
                xlsWriteLabel($xlsRow,51,$row_veh['sim_num']);
                xlsWriteLabel($xlsRow,52,$row_veh['device']);
                
                //OTHER DETAILS
				xlsWriteLabel($xlsRow,53,$fcard);
                xlsWriteLabel($xlsRow,54,$row_veh['id_no']);
                xlsWriteLabel($xlsRow,55,$row_veh['band']);
                xlsWriteLabel($xlsRow,56,$row_veh['benefit_plan']);
                xlsWriteLabel($xlsRow,57,$row_veh['cost_center']);
                xlsWriteLabel($xlsRow,58,$row_veh['group1']);
                xlsWriteLabel($xlsRow,59,$row_veh['division']);
                xlsWriteLabel($xlsRow,60,$row_veh['section']);
                xlsWriteLabel($xlsRow,61,$row_veh['ref_po']);
                xlsWriteLabel($xlsRow,62,$row_veh['asset_no']);
                xlsWriteLabel($xlsRow,63,$row_veh['equipt_no']);
                
                $xlsRow++;
				}                    
                xlsEOF();
                exit();
?>